package org.loofer.ext.utils;

import android.support.annotation.Nullable;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Comparator;
import org.apache.log4j.spi.Configurator;

public final class ObjectUtils {
    private static final String[] types = new String[]{"int", "java.lang.String", "boolean", "char", "float", "double", "long", "short", "byte"};

    public static <T> String objectToString(@Nullable T object) {
        if (object == null) {
            return "Object{object is null}";
        }
        if (!object.toString().startsWith(object.getClass().getName() + "@")) {
            return object.toString();
        }
        StringBuilder builder = new StringBuilder(object.getClass().getSimpleName() + "{");
        for (Field field : object.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            boolean flag = false;
            String[] strArr = types;
            int length = strArr.length;
            int i = 0;
            while (i < length) {
                if (field.getType().getName().equalsIgnoreCase(strArr[i])) {
                    flag = true;
                    Object value = null;
                    String str;
                    Object[] objArr;
                    String str2;
                    try {
                        value = field.get(object);
                        str = "%s=%s, ";
                        objArr = new Object[2];
                        objArr[0] = field.getName();
                        if (value == null) {
                            str2 = Configurator.NULL;
                        } else {
                            str2 = value.toString();
                        }
                        objArr[1] = str2;
                        builder.append(String.format(str, objArr));
                    } catch (IllegalAccessException e) {
                        IllegalAccessException value2 = e;
                        str = "%s=%s, ";
                        objArr = new Object[2];
                        objArr[0] = field.getName();
                        if (value2 == null) {
                            str2 = Configurator.NULL;
                        } else {
                            str2 = value2.toString();
                        }
                        objArr[1] = str2;
                        builder.append(String.format(str, objArr));
                    } catch (Throwable th) {
                        str = "%s=%s, ";
                        objArr = new Object[2];
                        objArr[0] = field.getName();
                        if (value == null) {
                            str2 = Configurator.NULL;
                        } else {
                            str2 = value.toString();
                        }
                        objArr[1] = str2;
                        builder.append(String.format(str, objArr));
                    }
                    if (!flag) {
                        builder.append(String.format("%s=%s, ", new Object[]{field.getName(), "Object"}));
                    }
                } else {
                    i++;
                }
            }
            if (!flag) {
                builder.append(String.format("%s=%s, ", new Object[]{field.getName(), "Object"}));
            }
        }
        return builder.replace(builder.length() - 2, builder.length() - 1, "}").toString();
    }

    public static <T> int compare(T a, T b, Comparator<? super T> c) {
        if (a == b) {
            return 0;
        }
        return c.compare(a, b);
    }

    public static boolean deepEquals(Object a, Object b) {
        if (a == null || b == null) {
            if (a == b) {
                return true;
            }
            return false;
        } else if ((a instanceof Object[]) && (b instanceof Object[])) {
            return Arrays.deepEquals((Object[]) a, (Object[]) b);
        } else {
            if ((a instanceof boolean[]) && (b instanceof boolean[])) {
                return Arrays.equals((boolean[]) a, (boolean[]) b);
            }
            if ((a instanceof byte[]) && (b instanceof byte[])) {
                return Arrays.equals((byte[]) a, (byte[]) b);
            }
            if ((a instanceof char[]) && (b instanceof char[])) {
                return Arrays.equals((char[]) a, (char[]) b);
            }
            if ((a instanceof double[]) && (b instanceof double[])) {
                return Arrays.equals((double[]) a, (double[]) b);
            }
            if ((a instanceof float[]) && (b instanceof float[])) {
                return Arrays.equals((float[]) a, (float[]) b);
            }
            if ((a instanceof int[]) && (b instanceof int[])) {
                return Arrays.equals((int[]) a, (int[]) b);
            }
            if ((a instanceof long[]) && (b instanceof long[])) {
                return Arrays.equals((long[]) a, (long[]) b);
            }
            if ((a instanceof short[]) && (b instanceof short[])) {
                return Arrays.equals((short[]) a, (short[]) b);
            }
            return a.equals(b);
        }
    }

    public static boolean equals(Object a, Object b) {
        if (a == null) {
            return b == null;
        } else {
            return a.equals(b);
        }
    }

    public static int hash(Object... values) {
        return Arrays.hashCode(values);
    }

    public static int hashCode(Object o) {
        return o == null ? 0 : o.hashCode();
    }

    public static <T> T requireNonNull(T o) {
        if (o != null) {
            return o;
        }
        throw new NullPointerException();
    }

    public static <T> T requireNonNull(T o, String message) {
        if (o != null) {
            return o;
        }
        throw new NullPointerException(message);
    }

    public static String toString(Object o) {
        return o == null ? Configurator.NULL : o.toString();
    }

    public static String toString(Object o, String nullString) {
        return o == null ? nullString : o.toString();
    }
}
